package com.esd.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.esd.crm.entity.Project;
import com.esd.crm.service.ProjectService;
import com.esd.crm.util.MailUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务类
 */
//要声明为bean，没有声明启动类启动无法实现定时效果
  @Slf4j
  @Component
  public class SchedulerTask {

    //表示每天1点进行一次处理
//    @Scheduled(cron = "0 0 1 * * ?")
//    System.out.println("this is a scheduler task running:"+(count++));
	
  @Autowired
  ProjectService projectService;
	 
  private  Logger logger = Logger.getLogger(SchedulerTask.class);
	  
//	@Scheduled(cron = "0/5 * * * * *")
    @Scheduled(cron = "0 0 1 * * ?")
	 private void proces(){
		//获取需要的信息
        List<Project> projects = projectService.selectAllProject();
        //日期格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
	    //遍历，逐条判断
	    for(Project project:projects){
	    	
	    	/*System.out.println(project.toString());
	    	System.out.println();*/
	    	
	    	//合同编号
	    	String num = project.getContractnum();
	    	//项目名称
	    	String name = project.getProjectname();
	    	//项目实施时间
	    	Date a1 = project.getImplementationdate();
	    	//现在时间
	    	Date b1 = new Date();
	    	//免维护时间
	    	int c1;
	    	String c = project.getFreemaintenance();
	    	if( c==null || "0".equals(c) ){ 
	    		c1=999;
	    	}else if( "e".equals(c) ){
	    		c1=0;
	    	}else{
	    		c1=Integer.parseInt(c);
	    	}
	    	System.out.println("c1:"+c1);
	    	
	    	//对日期进行加减运算
	    	Calendar cal = Calendar.getInstance();
	    	cal.setTime(a1);
	    	cal.add(Calendar.YEAR, c1);
	    	
	    	//int版==实施时间+免维时间
	    	Date a2=cal.getTime();
	    	String a3=sdf.format(a2);
	    	int a4 = Integer.parseInt(a3);
	    	System.out.println("实施时间+免维时间:"+a4);
	    	
	    	//int版==现在时间
	    	String b2=sdf.format(b1);
	    	int b3 = Integer.parseInt(b2);
	    	System.out.println("现在时间:"+b3);
	    	
	    	String language = "提示：合同名称:<"+name+">的维护期已到，请查看合同信息，进行收账交流！";
	    	
	    	if(a4==b3){
	            MailUtils.send("240319849@qq.com", language);
	            logger.info(name+"====1邮件已发送！");
	          }else if( ( (double)(b3-a4)/10000 ) - (int)((b3-a4)/10000)==0 && b3>a4 ){
	            MailUtils.send("240319849@qq.com", language);
	            logger.info(name+"====2邮件已发送！");
	          }
//	    	if(a4==b3){
//	    		MailUtils.send("240319849@qq.com", language);
//	    		System.out.println(name+"====1邮件已发送！");
//	    	}else if(  (int)((b3-a4)/10000)-((b3-a4)/10000)==0 && b3>a4 ){
//	    		MailUtils.send("240319849@qq.com", language);
//	    		System.out.println(name+"=====2邮件已发送！");
//	    	}
	    	
	    }//for	
	    }//方法proces
  }//类
